import {defaultBefore,request} from "@/utils/ajax.js"
import { getBaseUrl } from "@/utils/config"
// #ifndef MP-WEIXIN
export const payModes = {
	1:{name:"支付宝",provider:'alipay',key:1,class:'iconfont icon-alipay text-blue'},
	2:{name:"微信",provider:'wxpay',key:2,class:'iconfont icon-wxpay text-green'}
}
// #endif

// #ifdef MP-WEIXIN
export const payModes = {
	2:{name:"微信",provider:'wxpay',key:2,class:'iconfont icon-wxpay text-green'}
}
// #endif

export const requestPayment= async (data)=>{
	return new Promise((resolve,reject)=>{
		uni.requestPayment({
			...data,
			success: resolve,
			fail: reject
		})
	})
}
export const lowerKeys = (obj) =>{
	return Object.fromEntries(Object.entries(obj).map(([key,value])=>[key.toLowerCase(),value]))
}
export const payment = async (orderId,payplatForm)=>{
	try{
		if(payment.lock){
			throw new Error('正在请求中')
		}
		payment.lock=true;
		
		if(payplatForm==3){ 
			const res = await walletPay(orderId)
			return res
		}
		const options = await defaultBefore({
				url:`${getBaseUrl()}/payInfo/pay`,
				method:'GET',
				data:{
					orderId,
					payplatForm,
					// #ifdef APP-PLUS
					payplatType:1
					// #endif
					// #ifdef H5
					payplatType:2
					// #endif
					// #ifdef MP
					payplatType:3
					// #endif
				}
		})
		console.log(options) 
		// #ifdef H5
		console.log(options)
		window.open(options.url)
		return true
		// #endif
		uni.showLoading({
			mask:true,
			title:'加载中...'
		})
		const {data,statusCode} = await request(options)
		console.log(data,statusCode)
		uni.hideLoading()
		if(statusCode==200){
			let orderInfo = data;
			// #ifdef APP-PLUS
			
			if(payplatForm==2){
				orderInfo = lowerKeys(orderInfo)
			}
			// #endif
			
			return requestPayment({
				// #ifdef MP-WEIXIN
					...data,
				// #endif
				orderInfo,
				provider: payModes[payplatForm].provider,
			});
		}else{
			throw new Error('支付失败')
		}
	}catch(e){
		uni.hideLoading()
		throw e;
	}finally{
		payment.lock=false;
	}
}
export const openVip = async ({userId,vipId,payplatForm})=>{
	const options = await defaultBefore({
			url:`${getBaseUrl()}/vipOpenLog/openVip`,
			method:'GET',
			data:{
				userId,
				vipId,
				payplatForm,
				// #ifdef APP-PLUS
				payplatType:1
				// #endif
				// #ifdef H5
				payplatType:2
				// #endif
				// #ifdef MP
				payplatType:3
				// #endif
			}
	})
	console.log(options) 
	// #ifdef H5
	console.log(options)
	window.open(options.url)
	return true
	// #endif
  
	try{
		if(openVip.lock){
			throw new Error('正在请求中')
		}
		openVip.lock=true;
		uni.showLoading({
			mask:true,
			title:'加载中...'
		})
		const {data,statusCode} = await request(options)
		console.log(data,statusCode)
		uni.hideLoading()
		if(statusCode==200){
			let orderInfo = data;
			// #ifdef APP-PLUS
			
			if(payplatForm==2){
				orderInfo = lowerKeys(orderInfo)
			}
			// #endif
			
			return requestPayment({
				// #ifdef MP-WEIXIN
					...data,
				// #endif
				orderInfo,
				provider: payModes[payplatForm].provider,
			});
		}else{
			throw new Error('支付失败')
		}
	}catch(e){
		uni.hideLoading()
		throw e;
	}finally{
		openVip.lock=false;
	}
}
export const walletPay = (orderId)=>{
	return ajax({
		url:`/wallet/pay`,
		data:{
			orderId
		}
	})
}
export default{
	payment,
	openVip
}